package com.snack.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.snack.common.result.Result;
import com.snack.entity.pojo.SalesReports;
import com.snack.mapper.SalesReportsMapper;
import com.snack.service.SalesReportsService;
import com.snack.service.SnackService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * @author huangfuhao
 * @Date 2025/4/19
 */
@Service
public class SalesReportsImpl extends ServiceImpl<SalesReportsMapper, SalesReports> implements SalesReportsService {

    @Resource
    private SnackService snackService;

    @Override
    public Result getSalesReportsByDateRange(LocalDate startDate, LocalDate endDate, Long storeId) {
        QueryWrapper<SalesReports> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("report_date", startDate, endDate);

        if (storeId != null) {
            queryWrapper.eq("store_id", storeId);
        }

        List<SalesReports> salesReportsList = list(queryWrapper);
        if (salesReportsList.isEmpty()) {
            return Result.success();
        }
        //遍历设置零食名称
        salesReportsList.forEach(salesReports -> {
            salesReports.setBestSellingName(snackService.getById(salesReports.getBestSellingProductId()).getName());
            salesReports.setWorstSellingName(snackService.getById(salesReports.getWorstSellingProductId()).getName());
        });
        return Result.success(salesReportsList);
    }
}
